<?php

namespace Drinkiit\BlogBundle\Handler;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;

use Drinkiit\BlogBundle\Entity\Produit;

class AdminProduitHandler
{
    protected $form;
    protected $request;
    protected $em;
    protected $controller;

    public function __construct(Form $form, Request $request, Controller $controller, EntityManager $em)
    {
        $this->form    = $form;
        $this->request = $request;
        $this->em      = $em;
        $this->controller = $controller ;
    }

    public function process()
    {
        if( $this->request->getMethod() == 'POST' and !$this->request->query->has('id') )
        {
            $this->form->bindRequest($this->request);

            if( $this->form->isValid() )
            {
				echo $n = $this->form->get('prix')->getData()*100 ;
				$this->form->get('prix')->setData($n) ;
				echo $this->form->get('prix')->getData() ;
				
				$this->controller->get('logger')->info("[INFO] On a bien ajouté le produit n°".$this->form->getData()->getId());
                $this->onSuccess($this->form->getData());
				
                return true;
            }
        }

        return false;
    }

    public function onSuccess($objet)
    {
        $this->em->persist($objet);
        $this->em->flush();
    }
}
